
import { Switch, Route, Redirect,withRouter } from "react-router-dom";
import { useinfo } from '../utils/useInfo'


function RouterView(props) {
    const { routes } = props
    return <Switch>
        {

            routes.map((item,index)=>{
                if(item.component){
                    return <Route key={index} exact={item.exact} path={item.path} render={
                        ()=>{//render属性添加title
                            if(item.meta.mustLogin){                  
                               if(!useinfo().token){
                               return  <Route key={index} exact={item.exact} path={item.path}>
                                   <Redirect to={'/login'}/>       
                                       </Route>
                       }             
                   }
                           item.meta.title&&(document.title=item.meta.title)//设置title
                           var Component=withRouter(item.component) //添加路由属性
                           return <Component {...props}></Component>
   
                       }
                    } />
                }else{

                    return <Route key={index} exact={item.exact} path={item.path}>
                        <Redirect to={item.redirect} />
                    </Route>
                }

            })
        }
    </Switch>
}

export default RouterView